package org.syncsoft.overalt.socket;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 
 * @author tptfc
 *
 */
public class ServerSocketImpl {
	private ServerSocket serverSocket;
	private ExecutorService threadPool;
	private boolean running;
	
	public ServerSocketImpl() throws IOException {
		setThreadPool(Executors.newFixedThreadPool(60));
		setServerSocket(new ServerSocket(1003));
	}
	
	public void start() {
		setRunning(true);
		try {
			while (isRunning()) {
				Socket socket = getServerSocket().accept();
				getThreadPool().execute(new ListenerSocket(socket));
			} 
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private ServerSocket getServerSocket() {
		return serverSocket;
	}
	private void setServerSocket(ServerSocket serverSocket) {
		this.serverSocket = serverSocket;
	}
	private boolean isRunning() {
		return running;
	}
	private void setRunning(boolean running) {
		this.running = running;
	}

	private ExecutorService getThreadPool() {
		return threadPool;
	}

	private void setThreadPool(ExecutorService threadPool) {
		this.threadPool = threadPool;
	}
	
	public static void main(String[] args) throws IOException {
		ServerSocketImpl server = new ServerSocketImpl();
		server.start();
	}
}
